package com.damon.JDBC;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

import org.slf4j.Logger;

import com.damon.entity.MonitorDeviceEntity;
import com.damon.log.WcsLog;
import com.damon.utils.DbUtil;
import com.damon.utils.LogUtil;

public class MonitorDeviceJDBC {
//	public static WcsLog logger = LogUtil.getLogger(MonitorDeviceJDBC.class);
	/**
	 * 更新一条数据
	 * @param deviceEntity
	 * @return
	 * @throws SQLException
	 */
	public static int updateOne(MonitorDeviceEntity deviceEntity) throws SQLException {
		//更新数据
		Connection conn = DbUtil.getConnection();
		PreparedStatement updateStatement = null;
		try {
//			conn.setAutoCommit(false);
			String updateCountSql= "update wcs_monitor_device set device_state=?,update_time=GETDATE() "
					+ "where device_type=? "
					+ "and device_code=?";
				String deviceState = deviceEntity.getDeviceState();
				String deviceCode = deviceEntity.getDeviceCode();
				String deviceType = deviceEntity.getDeviceType();
//				logger.info("deviceState:"+deviceState);
//				logger.info("deviceType:"+deviceType);
//				logger.info("deviceCode:"+deviceCode);
				updateStatement = conn.prepareStatement(updateCountSql);
				updateStatement.setString(1	, deviceState);
				updateStatement.setString(2	, deviceType);
				updateStatement.setString(3	, deviceCode);
				updateStatement.executeUpdate();
			
		}
		finally {
			try {
				if (updateStatement!=null) {
					updateStatement.close();
				}
				if(conn!=null) {
					conn.close();
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	
		
		return 1;
	
		
	}
	
	/**
	 * 	更新多条数据
	 * @param entityList
	 * @return
	 * @throws SQLException
	 */
	public static int updateAll(ArrayList<MonitorDeviceEntity> entityList) throws SQLException {

		int count = 0;
		//更新数据
		Connection conn = DbUtil.getConnection();
		PreparedStatement updateStatement = null;
		try {
//			conn.setAutoCommit(false);
			String updateCountSql= "update wcs_monitor_device set device_state=?,update_time=GETDATE() "
					+ "where device_type=? "
					+ "and device_code=?";
			for(MonitorDeviceEntity entity : entityList) {
//				long start = System.currentTimeMillis();
				String deviceState = entity.getDeviceState();
				String deviceCode = entity.getDeviceCode();
				String deviceType = entity.getDeviceType();
//				logger.info("deviceState:"+deviceState);
//				logger.info("deviceType:"+deviceType);
//				logger.info("deviceCode:"+deviceCode);
				updateStatement = conn.prepareStatement(updateCountSql);
				updateStatement.setString(1	, deviceState);
				updateStatement.setString(2	, deviceType);
				updateStatement.setString(3	, deviceCode);
				updateStatement.executeUpdate();
				count++;
//				logger.info("更新一波用时："+(System.currentTimeMillis()-start));
			}
			
		}
		finally {
			try {
				if (updateStatement!=null) {
					updateStatement.close();
				}
				if(conn!=null) {
					conn.close();
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	
		
		return count;
	}

	/**
	 * 根据数据类型，更新数据
	 * @param type
	 * @param entityList
	 * @return
	 * @throws SQLException
	 */
	public static int updateMoreByType(String type, ArrayList<MonitorDeviceEntity> entityList) throws SQLException {

		int count = 0;
		// 更新数据
		Connection conn = DbUtil.getConnection();
		Statement updateStatement0 = null;
		Statement updateStatement1 = null;
		try {
//			conn.setAutoCommit(false);
			StringBuffer buf0 = new StringBuffer();
			StringBuffer buf1 = new StringBuffer();
			for (MonitorDeviceEntity entity : entityList) {
				if ("0".equals(entity.getDeviceState())) {
					buf0.append(entity.getDeviceCode() + ",");
				}

				if ("1".equals(entity.getDeviceState())) {
					buf1.append(entity.getDeviceCode() + ",");
				}

			}
			//更新state为0的数据
//			buf0.deleteCharAt(buf0.lastIndexOf(","));
			//追加999，这个方式一是为保证in（）里尾部不是‘,’,二是为了防止in()里数据为空
			buf0.append("999");
			String inSql0 = buf0.toString();
			
			
			String updateCountSql0 = "update wcs_monitor_device set device_state=0,update_time=GETDATE() "
					+ "where device_type=" + type + " and device_code in (" + inSql0 + ")";
			System.out.println(updateCountSql0);
			updateStatement0 = conn.createStatement();
			count += updateStatement0.executeUpdate(updateCountSql0);

//			//更新state为1的数据
//			if(buf1.lastIndexOf(",")!=-1) {
//				buf1.deleteCharAt(buf1.lastIndexOf(","));
//			}
			buf1.append("999");
			String inSql1 = buf1.toString();
			
			String updateCountSql1 = "update wcs_monitor_device set device_state=1,update_time=GETDATE() "
					+ "where device_type=" + type + " and device_code in (" + inSql1 + ")";
			System.out.println(updateCountSql1);

			updateStatement1 = conn.createStatement();
			count += updateStatement1.executeUpdate(updateCountSql1);

		} finally {
			try {
				if (updateStatement0 != null) {
					updateStatement0.close();
				}
				if (updateStatement1 != null) {
					updateStatement1.close();
				}
				if (conn != null) {
					conn.close();
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return count;
	}
}
